package cn.wq3stone.service.impl;

import cn.wq3stone.dao.UserDao;
import cn.wq3stone.dao.impl.UserDaoImpl;
import cn.wq3stone.exception.LoginException;
import cn.wq3stone.exception.PasswordNullException;
import cn.wq3stone.exception.RegisterException;
import cn.wq3stone.exception.UsernameNullException;
import cn.wq3stone.pojo.User;
import cn.wq3stone.service.UserService;

/**
 * @ClassName UserServiceImpl.java
 * @Description
 * @Author cn.wq3stone
 * @Date 2023/4/24 22:13
 */
public class UserServiceImpl implements UserService {
    private final UserDao userDaoImpl = new UserDaoImpl();

    /**
     * 注册
     * @param username 用户名
     * @param password 密码
     */
    public void register(String username,String password,String password2) throws PasswordNullException, RegisterException {
        if(password==null){
            throw new PasswordNullException("密码为空");
        }
        if(password2==null){
            throw new PasswordNullException("确认密码为空");
        }
        if(!password.equals(password2)){
            throw new RegisterException("两次密码输入不一致");
        }
        userDaoImpl.addUser(new User(username,password));
    }

    public User login(String username,String password) throws UsernameNullException, LoginException {
        User user = userDaoImpl.queryByUame(username);
        if(user==null){
            throw new UsernameNullException("账号不存在");
        }
        if(!user.getPassword().equals(password)){
            throw new LoginException("账号或密码错误");
        } else {
            return new User(user.getId(),user.getUsername(),user.getPassword());
        }
    }
}
